package utils;

import com.ibm.icu.text.CollationKey;
import com.ibm.icu.text.Collator;
import com.ibm.icu.util.ULocale;

import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CommonUtil {

    public static int screenWidth = java.awt.Toolkit.getDefaultToolkit().getScreenSize().width;
    public static int screenHeight = java.awt.Toolkit.getDefaultToolkit().getScreenSize().height;
    private static PropertiesUtils settings = new PropertiesUtils("SETTINGS.properties");
    public static Color color = new Color(Integer.parseInt(settings.get("font.r")),Integer.parseInt(settings.get("font.g")),Integer.parseInt(settings.get("font.b")));

    public static void sortStrList(List<String> list){
        list.sort(new Comparator<String>() {
            Collator collator = Collator.getInstance(ULocale.SIMPLIFIED_CHINESE);
            @Override
            public int compare(String o1, String o2) {
                CollationKey key1 = collator.getCollationKey(o1);
                CollationKey key2 = collator.getCollationKey(o2);
                return key1.compareTo(key2);
            }
        });
    }

    public static String encode(String url)    {
        try {
            Matcher matcher = Pattern.compile("^(\\d+)[\u4E00-\u9FA5]{3}$").matcher(url);
            int count = 0;
            while (matcher.find()) {
                String tmp=matcher.group();
                url=url.replaceAll(tmp,java.net.URLEncoder.encode(tmp,"gbk"));
            }
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return url;
    }

    public static void formatLine(List<String> baseList, List<String> titleList, List<List<String>> lineList, List<List<String>> pathList){
        List<String> lineCache = new ArrayList<>();
        List<String> pathCache = new ArrayList<>();
        List<String> lastLine = new ArrayList<>();
        List<String> lastPath = new ArrayList<>();
        for (String base : baseList){
            if (base.length()<5){
                if (titleList.size()==0){
                    titleList.add(base);
                }else {
                    lineList.add(lineCache);
                    pathList.add(pathCache);
                    titleList.add(base);
                    lineCache = new ArrayList<>();
                    pathCache = new ArrayList<>();
                    lastLine.clear();
                    lastPath.clear();
                }
            }else if (base.contains("http")){
                pathCache.add(base);
                lastPath.add(base);
            }else {
                lineCache.add(base);
                lastLine.add(base);
            }
        }
        lineList.add(lastLine);
        pathList.add(lastPath);
    }

    public static void formatLine(List<String> baseList, List<String> lineList, List<String> pathList){
        for (int i=0;i<baseList.size();i++){
            String base = baseList.get(i);
            if (base.contains("http")){
                pathList.add(base);
                lineList.add(baseList.get(i-1));
            }
        }
    }

    public static BufferedImage getFlippedImage(BufferedImage bi) {
        BufferedImage flipped = new BufferedImage(bi.getWidth(), bi.getHeight(), bi.getType());
        AffineTransform tran = AffineTransform.getTranslateInstance(bi.getWidth(), 0);
        AffineTransform flip = AffineTransform.getScaleInstance(-1d, 1d);
        tran.concatenate(flip);

        Graphics2D g = flipped.createGraphics();
        g.setTransform(tran);
        g.drawImage(bi, 0, 0, null);
        g.dispose();

        return flipped;
    }

    public static Font getFont(){
        return new Font("微软雅黑", Font.PLAIN, 16);
    }

    public static void removeDuplicate(List<String> list) {
        HashSet<String> h = new HashSet(list);
        list.clear();
        list.addAll(h);
    }

    public static List<String> filterList(List<String> list, String str){
        List<String> resultList = new ArrayList<>();
        for (String cache : list){
            if (cache.contains(str)){
                resultList.add(cache);
            }
        }
        return resultList;
    }

}
